﻿using Nova_BLL;
using Nova_Common.Extend;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WMS_Api.App_Start;
using WMS_Api.Areas.RFSystem.Models;
using WMS_Api.Controllers;
using WMS_Api.Models;

namespace WMS_Api.Areas.RFSystem.Controllers
{
    /// <summary>
    /// 拣选操作
    /// </summary>
    [RoutePrefix("api/RFSystem/ChooseStorage")]
    [ControllerGroup("PDA手持", "拣选操作功能接口")]
    public class ChooseStorageController : BaseApiController
    {
        RFBll RfBll = new RFBll();

        /// <summary>
        /// 根据托盘码获取指令,物料等出库信息
        /// </summary>
        /// <param name="trayId">托盘码</param>
        /// <returns></returns>
        [HttpGet, Route("GetChooseCommandMatInfoByTrayId")]
        public ApiResult GetChooseCommandMatInfoByTrayId(string trayId)
        {
            if (trayId.IsEmpty())
                return Error(ResultCode.ParaError, "托盘码不能为空!");
            DataTable dt = RfBll.GetChooseCommandMatInfoByTrayId(trayId);
            List<TrayChooseStorageCommandModel> list = Common.DataTableToList<TrayChooseStorageCommandModel>(dt);
            var model = list?.FirstOrDefault();
            return Success("根据托盘码获取拣选指令,物料等出库信息!", model);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost,Route("EditChooseMatNum")]
        public ApiResult EditChooseMatNum(ChoosekMatInfoModel param)
        {
          if(param.TaskId.IsEmpty())
                return Error(ResultCode.ParaError, "拣选任务编号不能为空!");
            TCommandsDetailBll detaillBll = new TCommandsDetailBll();
            var entity = detaillBll.GetByTaskId(long.Parse(param.TaskId));
            if(entity.IsEmpty())
                return Error(ResultCode.ParaError, "拣选任务不存在!");
            entity.MAT_NUM = param.MatNum;
            bool isUpdate = detaillBll.UpdateEntity(entity);
            return isUpdate ? Success() : Error("修改拣选数量失败!");
        }
    }
}
